import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Game } from "../Game";
import { DataEvent } from "../events/DataEvent";
import { AIManager } from "../scene/ai/AIManager";
import { GameController } from "../mvc/GameController";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
import { IView } from "../../mvc/interfaces/IView";
//class GuidePathController
    
    export  class GuidePathController extends Controller
    {
       

        /* internal  */initListener(): void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.AI_NpcInDestance, this.onInNpcDistanceHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.PLAYER_FIRE, this.onPlayerFireHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWorkEndHandler);
            return;
        }

        /* internal  */removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.removeEventListener(EventName.AI_NpcInDestance, this.onInNpcDistanceHandler);
            RolePlayer.instance.removeEventListener(PlayerEvent.PLAYER_FIRE, this.onPlayerFireHandler);
            RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWorkEndHandler);
            return;
        }

        /* internal  */endGuidePath(): void
        {
            this.removeListener();
            cache.guidePath.clearPath();
            this.clearPath();
            return;
        }

        /* internal  */drawPath(): void
        {
            if (cache.guidePath.needDrawPath && !(cache.guidePath.needDrawPath.length == 0)) 
            {
                Game.scene.pathLayer.updatePathPoint(cache.guidePath.needDrawPath);
            }
            else 
            {
                this.clearPath();
            }
            return;
        }

        /* internal  */clearPath(): void
        {
            Game.scene.pathLayer.clearPathPoint();
            return;
        }

        /* internal  */checkPathDraw(): void
        {
            if (cache.guidePath.isNeedDraw()) 
            {
                this.drawPath();
            }
            else 
            {
                this.clearPath();
            }
            return;
        }

        /* internal  */onGuidePathBeginHandler(arg1: DataEvent): void
        {
            this.initListener();
            this.clearPath();
            cache.guidePath.updatePath(arg1.data as Array<any>);
            this.checkPathDraw();
            return;
        }

        /* internal  */onInNpcDistanceHandler(arg1: DataEvent): void
        {
            if (cache.guidePath.guidePath) 
            {
                if (cache.dialog.npcInfo) 
                {
                    if (AIManager.checkLastTargetAsNPC(cache.dialog.npcInfo.tnpc.npcId)) 
                    {
                        this.endGuidePath();
                    }
                }
            }
            return;
        }

        /* internal  */onPlayerFireHandler(arg1: PlayerEvent): void
        {
            if (cache.guidePath.guidePath) 
            {
                if (GameController.scene.selectEntity && GameController.scene.selectEntity is MonsterPlayer) 
                {
                    if (AIManager.checkLastTargetAsBoss((GameController.scene.selectEntity as MonsterPlayer).bossCode)) 
                    {
                        this.endGuidePath();
                    }
                }
            }
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (cache.guidePath.guidePath) 
            {
                this.checkPathDraw();
            }
            return;
        }

        /* internal  */onGridWorkEndHandler(arg1: PlayerEvent): void
        {
            if (cache.guidePath.guidePath) 
            {
                this.checkPathDraw();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.GuidePathBegin, this.onGuidePathBeginHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.AI_NpcInDestance, this.onInNpcDistanceHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.PLAYER_FIRE, this.onPlayerFireHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWorkEndHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            return super.initView();
        }
    }
